// createRouter：创建路由实例对象；createWebHistory：创建history路由模式的实例对象
import {createRouter, createWebHistory} from 'vue-router'
import Layout from "@/views/layout/Layout.vue";
import Home from "@/views/content/Home.vue";
import ArticleInfo from "@/views/content/ArticleInfo.vue";
import Edit from "@/views/content/Edit.vue";
import UserSetting from "@/views/content/UserSetting.vue";
import ProfileSetting from "@/views/components/user/userSetting/ProfileSetting.vue";
import AccountSetting from "@/views/components/user/userSetting/AccountSetting.vue";
import CommonSetting from "@/views/components/user/userSetting/CommonSetting.vue";
import MessageSetting from "@/views/components/user/userSetting/MessageSetting.vue";
import User from "@/views/content/User.vue";
import UserArticleList from "@/views/components/user/UserArticleList.vue";
import UserFollowList from "@/views/components/user/UserFollowList.vue";
import UserFavoriteList from "@/views/components/user/UserFavoriteList.vue";
import UserLikeList from "@/views/components/user/UserLikeList.vue";

// 引入components


const router = createRouter({
    // 配置路由模式为history模式
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home',
            component: Layout,
            children: [
                {path: 'home', component: Home},
                {path: 'board/:boardId', component: Home},
                {path: 'search/:searchField', component: Home},
                {path: 'article/:articleId', component: ArticleInfo},
                {
                    path: 'user',
                    component: User,
                    children:[
                        {path:'/user/articleList',component:UserArticleList},
                        {path:'/user/favorite',component:UserFavoriteList},
                        {path:'/user/like',component:UserLikeList},
                        {path:'/user/follow',component:UserFollowList},
                    ],
                    meta: {title: '用户中心'},
                },
            ]
        },
        {
            path: '/edit',
            component: Edit
        },

        {
            path: '/setting',
            component: UserSetting,
            redirect: 'setting/profile',
            children: [
                {path: 'profile', component: ProfileSetting, meta: {title: '个人资料'}},
                {path: 'account', component: AccountSetting, meta: {title: '账号设置'}},
                {path: 'common', component: CommonSetting, meta: {title: '通用设置'}},
                {path: 'message', component: MessageSetting, meta: {title: '消息设置'}},
            ]
        }

    ],
    // 路由变化后将滚动条置顶
    scrollBehavior() {
        return {top: 0}
    }
})

/**
 * 前置路由守卫，路由跳转前触发
 */
router.beforeEach((to, from) => {
    // 修改页面title
    document.title = to?.meta.title || '大大怪论坛'

})


export default router